<template>
  <el-dialog :visible.sync="dialog" width="480px" :close-on-click-modal="false" :before-close="reset">
    <span slot="title" class="dialog-title">{{ title }}</span>
    <div class="dialog_info">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="团队名称" clearable />
        </el-form-item>
        <el-form-item label="部门编码" prop="deptCode">
          <el-input v-model="form.deptCode" placeholder="团队编码" clearable />
        </el-form-item>
        <el-form-item label="所属部门" prop="pid">
          <el-select v-model="form.pid" placeholder="部门" filterable clearable>
            <el-option v-for="item in deptList" :key="item.id" :label="item.deptName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="form.remark" placeholder="描述" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="reset">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { addDept, updateDept, getDepts } from '@/api/teams'
export default {
  name: 'UserDialog',
  props: {
    dialog: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      form: {
        status: 1,
        deptName: '',
        deptCode: '',
        remark: '',
        id: ''
      },
      title: '添加',
      rules: {
        deptName: [{ required: true, message: '请输入团队名称', trigger: 'blur' }],
        deptCode: [{ required: true, message: '请输入团队编码', trigger: 'blur' }]
      },
      deptList: []
    }
  },
  watch: {
    dialog(dialog) {
      if (dialog) {
        if (Object.keys(this.row).length === 0) {
          this.$data.form = this.$options.data().form
          this.title = '添加'
        } else {
          for (const key in this.form) {
            this.form[key] = this.row[key]
            this.title = '修改'
          }
        }
        this.getdeptList()
        this.form.status = this.form.status * 1
        setTimeout(() => {
          this.$refs.form.clearValidate()
        }, 10)
      }
    }
  },
  methods: {
    reset() {
      this.$emit('reset', 'dialog')
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateDept(this.form).then(res => {
              if (res.code * 1 === 200) {
                this.$message({
                  message: `${this.title}成功`,
                  type: 'success',
                  showClose: true
                })
                this.$emit('submit', 'dialog')
              }
            })
            return
          }
          addDept(this.form).then(res => {
            if (res.code * 1 === 200) {
              this.$message({
                message: `${this.title}成功`,
                type: 'success',
                showClose: true
              })
              this.$emit('submit', 'dialog')
            }
          })
        } else {
          return false
        }
      })
    },
    getdeptList() {
      const params = {
        page: 1,
        status: 1,
        pageSize: 999
      }
      getDepts(params).then(res => {
        this.deptList = res.data.row
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.dialog_info {
  .el-input,
  .el-select {
    width: 220px;
  }
}
</style>

